//
//  YFStatusCell.swift
//  微博
//
//  Created by 余仿 on 16/4/12.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SnapKit

/// 一条微博的cell
class YFStatusCell: UITableViewCell {


    var bottomCons:Constraint?
    /// ViewModel
    var statusViewModel:YFStatusViewModel?{

        didSet{

//http://ww3.sinaimg.cn/thumbnail/006h4oCqjw1f2ub4lol18j30jw0el74w.jpg
//http://ww3.sinaimg.cn/thumbnail/006h4oCqjw1f2ub4lol18j30jw0el74w.jpg
//            print(statusViewModel?.statusList?.pic_urls?.count)

            //给cell赋值
            originalView.statusViewModel = statusViewModel

            retweetView.statusViewModel = statusViewModel

            bottomView.statusViewModel = statusViewModel

            //底部工具条的约束参照设置
            setupBottomCons(statusViewModel!)
        }
    }

    func setupBottomCons(statusViewModel:YFStatusViewModel){

        //设置前先移除旧的
        bottomCons?.uninstall()
        //判断是否有转发微博
        if statusViewModel.statusList?.retweeted_status != nil{
            //存在转发微博,那么底部条的顶部约束参考应该为转发微博的底部
          bottomView.snp_updateConstraints(closure: { (make) -> Void in
               self.bottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
            retweetView.hidden = false
            })
        }else{
            //没有转发微博
          bottomView.snp_updateConstraints(closure: { (make) -> Void in
               self.bottomCons = make.top.equalTo(originalView.snp_bottom).constraint
            })
            retweetView.hidden = true
        }

    }

    /// 原创微博
    private lazy var originalView:YFStatusOriginalView = YFStatusOriginalView()

 /// 转发微博
    private lazy var retweetView:YFRetweetView = YFRetweetView()

    /// 底部条
    private lazy var bottomView:YFBottomView = YFBottomView()


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = .None
        //添加子控件
        contentView.addSubview(originalView)

        contentView.addSubview(retweetView)

        contentView.addSubview(bottomView)

        //布局
        setupUI()
        //绑定数据
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupUI(){

        originalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
//            make.height.equalTo(300)
        }
//        originalView.backgroundColor = UIColor.redColor()

        retweetView.snp_makeConstraints { (make) -> Void in

            make.top.equalTo(originalView.snp_bottom)

            make.left.right.equalTo(contentView)
        }
//        retweetView.backgroundColor = UIColor.yellowColor()



        bottomView.snp_makeConstraints { (make) -> Void in
            self.bottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
        }
//        bottomView.backgroundColor = UIColor.blueColor()

        //自动计算cell高度
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }

}
